/*
 * =====================================================================================
 *
 *       Filename:  set_filter.c
 *
 *    Description:  set filter
 *
 *        Version:  1.0
 *        Created:  2012年11月29日 10时27分10秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jianxi sun (jianxi), ycsunjane@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include <stdlib.h>
#include <stdio.h>
#include <pcap.h>

int main(int argc,char *argv[])
{
	pcap_t *handle;
	char dev[]="eth0";
	char errbuf[PCAP_ERRBUF_SIZE];
	struct bpf_program fp;
	char filter[]="port 23";
	bpf_u_int32 mask;
	bpf_u_int32 net;

	if(pcap_lookupnet(dev,&net,&mask,errbuf)==-1)
	{
		fprintf(stderr, "Can't get netmask for device %s\n", dev);
		net = 0;
		mask = 0;
	}

	handle = pcap_open_live(dev,BUFSIZ,1,1000,errbuf);
	if(handle == NULL)
	{
		fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
		return(2);
	}

	if(pcap_compile(handle,&fp,filter,0,mask)==-1)
	{
		fprintf(stderr, "Couldn't parse filter %s: %s\n", filter, pcap_geterr(handle));
		return(2);
	}
	if (pcap_setfilter(handle, &fp) == -1) {
		fprintf(stderr, "Couldn't install filter %s: %s\n", filter, pcap_geterr(handle));
		return(2);
	}
	return 0;
}
